﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccess;
using Utility;
using Data;

namespace Website.UC
{
    public partial class UCMajor : System.Web.UI.UserControl
    {
        Tbl_MajorBUS majorBus = new Tbl_MajorBUS();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                loadData();
            }
        }

        private void loadData()
        {
            GridView1.DataSource = majorBus.getListMajor();
            GridView1.DataBind();
        }

        protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            lblId.Text = WebHelper.GetValueGridView(GridView1, 2, e);
            txtName.Text = WebHelper.GetValueGridView(GridView1, 3, e);
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

            tbl_major tm = new tbl_major();
            tm.major_id = Int32.Parse(WebHelper.GetDataKey(GridView1 , e));
            majorBus.delete(tm);
            loadData();
            reset();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (txtName.Text.Length == 0)
            {
                WebHelper.ShowMessage("Name not null", Page);
            }
            else
            {
                tbl_major tm = new tbl_major();
                tm.major_name = txtName.Text;

                if (lblId.Text.Length == 0)
                {
                    majorBus.insertMajor(tm);
                }
                else
                {
                    tm.major_id = Int32.Parse(lblId.Text);
                    majorBus.updateMajor(tm);
                }
                reset();
                loadData();
            }


        }

        private void reset()
        {
            lblId.Text = "";
            txtName.Text = "";
        }

       
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            reset();
        }
    }
}